﻿using NewDevice;
using NewDevice.RS232;
using NewDevice.RS485;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Vision001.Models;
using Vision001.Procedure;

namespace Vision001.Helper
{
    /// <summary>
    /// 所有的全局变量，全部放在这里
    /// </summary>
    public static class Global

    {
        /// <summary>
        /// 所有的配置文件
        /// </summary>
        public static List<Config> _Configs;

        /// <summary>
        /// 加载的用户配置
        /// </summary>
        public static List<User> _Users;

        /// <summary>
        /// 当前选择的配置
        /// </summary>
        public static Config _Config;

        /// <summary>
        /// 海康相机
        /// </summary>
        public static HKCamera _HKcamera;

        public static HKCamera _HKcamera2;

        /// <summary>
        /// 系统是否初始化
        /// </summary>
        public static bool _isInit = false;

        /// <summary>
        /// 当前登录的用户
        /// </summary>
        public static User _User;

        /// <summary>
        /// xml文件配置路径
        /// </summary>
        public static string _ConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.xml");

        /// <summary>
        /// 系统配置文件中每一个sheet的名字
        /// </summary>
        public static string _IOconfigName = "IO定义";

        public static string _StationXYName = "工位坐标";

        public static string _TestProcedureconfigName = "测试流程";
        public static string _TestMethodconfigName = "测试方法";
        public static string _TestconfigName = "测试配置";

        /// <summary>
        /// 存放系统所有IO配置
        /// </summary>
        public static Dictionary<string, string> _IOconfigs;

        /// <summary>
        /// 存放系统所有测试配置。就是补偿
        /// </summary>
        public static Dictionary<string, int> _TestConfigs;

        /// <summary>
        /// 存放当前配置产品的工位坐标模板
        /// </summary>
        public static List<WorkStationXY> _StationXYConfigs;

        /// <summary>
        /// 存在系统所有测试流程
        /// </summary>
        public static List<TestMethodModel> _TestProcedureConfigs;

        /// <summary>
        /// 测试方法以及IO配置等路径
        /// </summary>
        public static string _SysConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sysconfig.xlsx");

        /// <summary>
        /// 当前用户是否登录
        /// </summary>
        public static bool _isLogin = false;

        /// <summary>
        /// 数据库客户端
        /// </summary>
        public static SqlSugarClient _sqlSugarClient;

        /// <summary>
        /// 读取所有设置方法
        /// </summary>
        //public static List<Action<TestMethodModel>> TestSetMethodModelActions;
        public static Dictionary<string, Action<TestMethodModel>> TestSetMethodModelActions;

        /// <summary>
        /// 读取所有读取方法
        /// </summary>
        //public static List<Func<TestMethodModel, bool>> TestGetMethodModelActions;
        public static Dictionary<string, Func<TestMethodModel, bool>> TestGetMethodModelActions;

        public static void ClearConfig()
        {
        }

        /// <summary>
        /// 每天生成的产能表初始数据
        /// </summary>
        public static List<CapacityData> _CapacityDatas = new List<CapacityData>()
        {
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "00:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "01:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "02:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "03:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "04:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "05:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "06:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "07:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "08:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "09:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "10:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "11:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "12:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "13:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "14:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "15:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "16:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "17:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "18:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "19:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "20:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "21:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "22:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "23:00"},
        };

        /// <summary>
        /// 程控电源
        /// </summary>
        public static IT6302 _It6302;

        /// <summary>
        /// 所有的采集模块
        /// </summary>
        public static List<ZS_AnalogInput> _ZsAnalogInputs;

        /// <summary>
        /// 通用IO采集模块
        /// </summary>
        public static PQW_IO _PqwIoCommon;

        /// <summary>
        /// 英鹏飞电机1
        /// </summary>
        public static YPF_40R _YPF_40R_1;

        /// <summary>
        /// 英鹏飞电机2
        /// </summary>
        public static YPF_40R _YPF_40R_2;

        /// <summary>
        /// 3.3V IO采集模块
        /// </summary>
        public static PQW_IO _PqwIoThreePointThree;

        /// <summary>
        /// 24V IO采集模块
        /// </summary>
        public static PQW_IO _PqwIoTwoFour;

        /// <summary>
        /// 所有采集模块的24V数据
        /// </summary>
        public static Dictionary<int, List<double>> _allZsAnalogOutputsDatas24 = new Dictionary<int, List<double>>();

        /// <summary>
        /// 所有采集模块的3.3V数据
        /// </summary>
        public static Dictionary<int, List<double>> _allZsAnalogOutputsDatas33 = new Dictionary<int, List<double>>();

        /// <summary>
        /// 所有工位的数据
        /// </summary>
        public static Dictionary<int, List<double>> _allWorkStationOutputsDatas = new Dictionary<int, List<double>>();

        /// <summary>
        /// 服务器
        /// </summary>
        public static SocketServer server;

        /// <summary>
        /// 机台运行状态
        /// </summary>
        public static bool isRunning = false;

        /// <summary>
        /// Tcp连接状态
        /// </summary>
        public static bool TcpIsConnec = false;

        /// <summary>
        /// 当前是否是io调试状态
        /// </summary>
        public static bool _isIOdebug = false;

        /// <summary>
        /// 当前是否是单步测试
        /// </summary>
        public static bool isOntStep = false;

        /// <summary>
        /// 是否是第一次手动
        /// </summary>
        public static bool isFirstOnStepExe = true;

        /// <summary>
        /// 测试机器是否要料
        /// </summary>
        public static bool TestRobotIsOK = false;

        /// <summary>
        /// 机台状态，是就绪还是等待
        /// </summary>
        public static string RobotStatus = "等待";

        /// <summary>
        /// 发送的二维码
        /// </summary>
        public static string RobotCode = "";

        /// <summary>
        /// 手动释放全局变量里的非托管资源
        /// </summary>
        public static void Dispose()
        {
            Global.server?.Close();
            Global._YPF_40R_1?.Stop();
            Global._YPF_40R_2?.Stop();

            Global._YPF_40R_1?.Dispose();
            Global._YPF_40R_2?.Dispose();

            _sqlSugarClient?.Dispose();
        }

        public static List<Config> Clone(this List<Config> obj)
        {
            List<Config> newConfigs = new List<Config>();
            foreach (var item in obj)
            {
                newConfigs.Add(item.Clone() as Config);
            }

            return newConfigs;
        }
    }
}